%%%-----------------------------------
%%% @Module  : pt_17
%%% @Author  : mxb
%%% @Created : 2016.08.25
%%% @Description: 17 兑奖
%%%-----------------------------------
-module(pt_17).    
-export([read/2, write/2]).   
-include("common.hrl").
-include("proto_all.hrl").


%%
%%客户端 -> 服务端 ----------------------------
%%

%% 17000 请求进入房间
read(17000, Bin) ->
	{Data, _} = proto_read:unpack(c2s_join_lot_room, Bin),
	{c2s_join_lot_room, UID} = Data,
	{ok, [UID]};

%% 17001 离开房间
read(17001, Bin) ->
	{Data, _} = proto_read:unpack(c2s_exit_lot_room, Bin),
	#c2s_exit_lot_room{uid = UID} = Data,
	{ok, [UID]};

%% 17002 押注
read(17002, Bin) ->
	{Data, _} = proto_read:unpack(c2s_make_bet, Bin),
	#c2s_make_bet{type = Type, goods_id = GoodsID, num = Num} = Data,
	{ok, [Type, GoodsID, Num]};

%% 17010 排行榜
read(17010, Bin) ->
	{Data, _} = proto_read:unpack(c2s_lottery_rank, Bin),
	#c2s_lottery_rank{type = Type} = Data,
	{ok, [Type]};

%% 17012 天降祥瑞活动状态
read(17012, Bin) ->
	{Data, _} = proto_read:unpack(c2s_good_omen_state, Bin),
	#c2s_good_omen_state{uid = UID} = Data,
	{ok, [UID]};

%% 17014 领取活动奖励
read(17014, Bin) ->
	{Data, _} = proto_read:unpack(c2s_get_good_omen_reward, Bin),
	#c2s_get_good_omen_reward{uid = UID, goods_list = GoodsList} = Data,
	%%io:format("~n====17014===GoodsList:~p====~n", [GoodsList]),
	NewGoodsList = [{GTID, Num} || {_, GTID, Num, _} <- GoodsList],
	{ok, [UID, NewGoodsList]};
	

%% %% 17011 幸运之星
%% read(17011, Bin) ->
%% 	{Data, _} = proto_read:unpack(c2s_lucky_star, Bin),
%% 	#c2s_lucky_star{uid = UID} = Data,
%% 	{ok, [UID]};

read(_Cmd, _R) ->
    ?ERROR_MSG("~s pt_17 reading cmd_[~p], Data:~p~n",[misc:time_format(game_timer:now()), _Cmd, _R]),
    {error, no_match}.


%%
%% 服务端 -> 客户端 ----------------------------
%%

%% 17000 请求进入房间返回
write(17000, [Seq, RecordData]) ->
	%%io:format("~n===write=======17000===========RecordData:~p========~n", [RecordData]),
	MsgPack = proto_write:pack(s2c_join_lot_room, RecordData),
	Data = <<Seq:32, MsgPack/binary>>,
	{ok, pt:pack(17000, Data)};

%% 17001 离开房间返回
write(17001, [Seq, RecordData]) ->	
	%%io:format("~n===write=======17001==========RecordData:~p========~n", [RecordData]),
	MsgPack = proto_write:pack(s2c_exit_lot_room, RecordData),
	Data = <<Seq:32, MsgPack/binary>>,
	{ok, pt:pack(17001, Data)};

%% 17002 押注
write(17002, [Seq, RecordData]) ->
	%%io:format("~n===write=======17002==========RecordData:~p========~n", [RecordData]),
	MsgPack = proto_write:pack(s2c_make_bet, RecordData),
	Data = <<Seq:32, MsgPack/binary>>,
	{ok, pt:pack(17002, Data)};

%% 17003 兑奖活动状态
write(17003, [Seq, RecordData]) ->
	%%io:format("~n===write=======17003==========RecordData:~p========~n", [RecordData]),
	MsgPack = proto_write:pack(s2c_lottery_state, RecordData),
	Data = <<Seq:32, MsgPack/binary>>,
	{ok, pt:pack(17003, Data)};

%% 17004 开奖
write(17004, [Seq, RecordData]) ->
	%%io:format("~n===write=======17004==========RecordData:~p========~n", [RecordData]),
	MsgPack = proto_write:pack(s2c_draw_lottery, RecordData),
	Data = <<Seq:32, MsgPack/binary>>,
	{ok, pt:pack(17004, Data)};

%% 17005 结算
write(17005, [Seq, RecordData]) ->
	%%io:format("~n===write=======17005==========RecordData:~p========~n", [RecordData]),
	MsgPack = proto_write:pack(s2c_get_reward, RecordData),
	Data = <<Seq:32, MsgPack/binary>>,
	{ok, pt:pack(17005, Data)};
	
%% 17006 通知玩家成为"幸运之星"
write(17006, [Seq, RecordData]) ->
	%%io:format("~n===write=======17006==========RecordData:~p========~n", [RecordData]),
	MsgPack = proto_write:pack(s2c_be_lucky_star, RecordData),
	Data = <<Seq:32, MsgPack/binary>>,
	{ok, pt:pack(17006, Data)};

%% 17007 通知所有玩家，有其他玩家押注
write(17007, [Seq, RecordData]) ->
	%%io:format("~n===write=======17007==========RecordData:~p========~n", [RecordData]),
	MsgPack = proto_write:pack(s2c_bc_make_bet, RecordData),
	Data = <<Seq:32, MsgPack/binary>>,
	{ok, pt:pack(17007, Data)};

%% 17008 通知所有玩家，有其他玩家退出房间
write(17008, [Seq, RecordData]) ->
	%%io:format("~n===write=======17008==========RecordData:~p========~n", [RecordData]),
	MsgPack = proto_write:pack(s2c_bc_leave_lottery, RecordData),
	Data = <<Seq:32, MsgPack/binary>>,
	{ok, pt:pack(17008, Data)};

%% 17009 更新胜率
write(17009, [Seq, RecordData]) ->
	%%io:format("~n===write=======17009==========RecordData:~p========~n", [RecordData]),
	MsgPack = proto_write:pack(s2c_win_rate, RecordData),
	Data = <<Seq:32, MsgPack/binary>>,
	{ok, pt:pack(17009, Data)};

%% 17010 排行榜
write(17010, [Seq, RecordData]) ->
	%%io:format("~n===write=======17010==========RecordData:~p========~n", [RecordData]),
	MsgPack = proto_write:pack(s2c_lottery_rank, RecordData),
	Data = <<Seq:32, MsgPack/binary>>,
	{ok, pt:pack(17010, Data)};

%% 17012 活动状态
write(17012, [Seq, RecordData]) ->
	MsgPack = proto_write:pack(s2c_good_omen_state, RecordData),
	Data = <<Seq:32, MsgPack/binary>>,
	{ok, pt:pack(17012, Data)};

%% 17013 刷新奖励
write(17013, [Seq, RecordData]) ->
	MsgPack = proto_write:pack(s2c_refresh_good_omen_reward, RecordData),
	Data = <<Seq:32, MsgPack/binary>>,
	{ok, pt:pack(17013, Data)};

%% 17014 领取奖励
write(17014, [Seq, RecordData]) ->
	MsgPack = proto_write:pack(s2c_get_good_omen_reward, RecordData),
	Data = <<Seq:32, MsgPack/binary>>,
	{ok, pt:pack(17014, Data)};


%% %% 17011 幸运之星
%% write(17011, [Seq, RecordData]) ->
%% 	io:format("~n===write=======17011==========RecordData:~p========~n", [RecordData]),
%% 	MsgPack = proto_write:pack(s2c_lucky_star, RecordData),
%% 	Data = <<Seq:32, MsgPack/binary>>,
%% 	{ok, pt:pack(17011, Data)};
  
write(Cmd, _R) ->
	%%io:format("~n===write===Cmd:~p====_R:~p========~n", [Cmd, _R]),
    ?ERROR_MSG("~s pt_17 writting cmd_[~p], Data:~p~n",[misc:time_format(game_timer:now()), Cmd, _R]),
    {ok, pt:pack(0, <<>>)}.

